package com.huafeng.listener;

import cn.hutool.json.JSONUtil;
import com.huafeng.entity.Request;
import com.huafeng.enums.RequestEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

/**
 * @author 花风
 * @date 2023/4/5 22:30
 * @Description 请求监听器
 */
@Slf4j
@Service
@com.huafeng.annotations.Listener(type = "request")
public class RequestListener implements Listener{

    @Override
    public Object parse(String messageJson) {
        Request request = JSONUtil.toBean(messageJson, Request.class);
        String requestType = request.getRequestType();

        Function<List<String>, Object> function = RequestEnum.get(requestType);
        String id = request.getGroupId() != null ? request.getGroupId() : request.getUserId();
        function.apply(Arrays.asList(id,request.getFlag(),request.getComment(),request.getSubType()));
        return null;
    }
}
